#define _SYS_GLOBE_VAR_						//系统外部声名定义 

#include "lib/includes.h"
#include "myutils.h"
#include "inirw.h"
#include "cputest.h"
#include "bustcp.h"
#include "ansrec.h"
#include "m1api.h"
#include "m1test.h"
#include "myother.h"
#include "getsys.h"
//中文为UTF-8编码，要正常显示需要UTF-8->GBK
//#define APP_VER  "模拟公交POS补采工具V1.0"
//#define POS_SoftVer     "BD_B502_1.22.11" 

//=====================================
char IPADDR[40];//服务器IP地址,  通信用
int  PORT;      //服务器IP端口

char IPADDR1[40];//服务器IP地址，读卡用
int  PORT1;      //服务器IP端口

char IPADDR2[40];//服务器IP地址，银联用
int  PORT2;      //服务器IP端口

char UPPosNum[20];    //银联终端号
char UPManageNum[40]; //银联商户号
U08  UPMainKey[40];  //银联主密钥

//=============================函数声明

void Com_Bus_TcpProc( void );

void Test_Write_OneRec(void);

extern int lua_test();//执行LUA脚本
//==========================================
int main(int argc, char* argv[])
{
	char disbuf[60]={0};
	int cmd=0,ret =0;
	static int logflag = 0;//日志开关
	/* Copyright */
 	printf("SimulatePOS  - Simple Collect Data Tool "PROGRAM_VERSION"\n"
	       "Copyright (c) Newcapec 2015-2016.\n");
 	printf("---------------------------------------------------\r\n");
 	LogFile = fopen( "error.log", "a" );//错误日志记录
    //errmsg( "%s %d %s", "Failed", 100, "times" );
	//u2g(APP_VER,sizeof(APP_VER),disbuf,sizeof(disbuf));
	//printf("=============%s===============\r\n",disbuf);
	APP_HookOpenMemory();
	//加载配置文件
	Load_ConfigIni();
	

	while(1)
	{
		cmd = 0;
		printf("===================================================\r\n");
		u2g("   [ 1]  消费记录分析",sizeof(disbuf),disbuf,sizeof(disbuf));
		printf("%s\r\n",disbuf);
		u2g("   [ 2]  写入一条记录",sizeof(disbuf),disbuf,sizeof(disbuf));
		printf("%s\r\n",disbuf);
		u2g("   [ 3]  远程M1卡消费",sizeof(disbuf),disbuf,sizeof(disbuf));
		printf("%s\r\n",disbuf);
		u2g("   [ 4]  远程CPU卡消费",sizeof(disbuf),disbuf,sizeof(disbuf));
		printf("%s\r\n",disbuf);
		u2g("   [ 5]  远程银联卡消费",sizeof(disbuf),disbuf,sizeof(disbuf));
		printf("%s\r\n",disbuf);
		u2g("   [ 6]  模拟POS采集通信",sizeof(disbuf),disbuf,sizeof(disbuf));
		printf("%s\r\n",disbuf);
		u2g("   [ 7]  模拟POS银联通信",sizeof(disbuf),disbuf,sizeof(disbuf));
		printf("%s\r\n",disbuf);
		u2g("   [ 8]  获取系统参数",sizeof(disbuf),disbuf,sizeof(disbuf));
		printf("%s\r\n",disbuf);
		u2g("   [ 9]  银联主密钥加载",sizeof(disbuf),disbuf,sizeof(disbuf));
		printf("%s\r\n",disbuf);
		if(logflag == 0)
		{
			u2g("   [10]  开启日志记录",sizeof(disbuf),disbuf,sizeof(disbuf));
			printf("%s\r\n",disbuf);
		}
		else
		{
			u2g("   [10]  关闭日志记录",sizeof(disbuf),disbuf,sizeof(disbuf));
			printf("%s\r\n",disbuf);
		}
		u2g("   [11]  执行LUA脚本",sizeof(disbuf),disbuf,sizeof(disbuf));
		printf("%s\r\n",disbuf);
		u2g("   [12]  终端初始化",sizeof(disbuf),disbuf,sizeof(disbuf));
		printf("%s\r\n",disbuf);
		u2g("   [ 0]  退出",sizeof(disbuf),disbuf,sizeof(disbuf));
		printf("%s\r\n",disbuf);
		printf("->");
		scanf("%d",&cmd);

		if(logflag == 1)//如果开启了日志记录
		{
			freopen("outlog.txt","a",stdout);//定向到文件
		}
		//printf("%d\r\n",cmd);
		switch(cmd)
		{
			case 1:
		    {
		    	AnsRec("rec01.bin",1,0);	
		    }break;
		    case 2:
			{
				Test_Write_OneRec();
			};break;
			case 3:
			{
				Hook_Debug_TxData("->连接服务器中...");
				printf("IP:%s,PORT:%d\r\n",IPADDR1,PORT1);
				if(Connect(IPADDR1,PORT1))
				{
					Hook_Debug_TxData("->连接失败!");
					break;
				}
				Hook_Debug_TxData("->连接成功!");
				Hook_Debug_TxData("->M1卡测试:");
				//读卡测试
                AH_M1Card_PrintAllData();
                //testcard_m1();
                Com_Dev_Disconnect(0);
				
			}break;	
			case 4:
			{
				Hook_Debug_TxData("->连接服务器中...");
				printf("IP:%s,PORT:%d\r\n",IPADDR1,PORT1);
				if(Connect(IPADDR1,PORT1))
				{
					Hook_Debug_TxData("->连接失败!");
					break;
				}
				Hook_Debug_TxData("->连接成功!");
				Hook_Debug_TxData("->CPU卡测试:");
				//读卡测试
                testcard_cpu();
                Com_Dev_Disconnect(0);
				
			}break;	
			case 5:
			{
				Hook_Debug_TxData("->连接服务器中...");
				printf("IP:%s,PORT:%d\r\n",IPADDR1,PORT1);
				if(Connect(IPADDR1,PORT1))
				{
					Hook_Debug_TxData("->连接失败!");
					break;
				}
				Hook_Debug_TxData("->连接成功!");
				Hook_Debug_TxData("->银联卡测试:");
				//读卡测试
                //UP_SysCfg.NormalOrShow = 1;
                TradeInformation.DealMoney = 1;
                get_time();
                Card_Proc_CUP();
                Com_Dev_Disconnect(0);
				
			}break;	
			case 6:
			{
				Com_Bus_TcpProc();
			};break;
			case 7:
			{
				UP_Comm_BehindOperation( 0 );
			};break;
			case 8:
			{
				SysDebug_get_sys(0,0);
			}break;		
			case 9:
			{
				ret = UP_Write_ComMainKek((U08*)UPManageNum, (U08*)UPPosNum, UPMainKey);
				if(ret == 0)
				{ Hook_Debug_TxData("->加载成功!");}
				else
				{ Hook_Debug_TxData("->加载失败!");}

			}break;
			case 10:
			{
				if(logflag == 0)
				{
					logflag = 1;
					freopen("outlog.txt","a",stdout); 
				}
				else
				{
					logflag = 0;
					freopen("CON", "w", stdout);
				}
			};break;
			case 11:
			{
				lua_test();
			}break;
			case 12:
			{
				Hook_Debug_TxData("->终端初始化中...");
				Init_Memory();
			}break;	
			case 0:  
			    fclose(stdout); exit(1);//输出重定向，输出数据将保存在out.txt文件中
			default:
				printf("Cmd is not right!\r\n");break;
		}
		freopen("CON", "w", stdout);

	}
}


void Com_Bus_TcpProc( void )
{
	U32 rcode;
R_Tx:
	Hook_Debug_TxData("->连接服务器中...");
	printf("IP:%s,PORT:%d\r\n",IPADDR,PORT);
	if(Connect(IPADDR,PORT))
	{
		Hook_Debug_TxData("->连接失败!");
		return;
	}
	Hook_Debug_TxData("<-连接成功!");
	rcode = Com_Bus_TcpSession();					//会话请求
	if( rcode != 0 ) 
	{
		if( rcode == 0x12345678 )					//特殊处理
		{ 
			Com_Dev_Disconnect( DEF_TcpChanNum );	//断开连接
			Hook_Debug_TxData("<-断开连接!");
			goto R_Tx; 
		}
		Com_Dev_Disconnect( DEF_TcpChanNum );	//断开连接
		Hook_Debug_TxData("<-断开连接!");
		DEF_SysCom_Debug; 
		return;
	}
	
	Hook_Debug_TxData("<-会话成功!");
	rcode = Com_Bus_TcpTerminus();					//上传终端信息
	if( rcode != 0 )
	{
		Hook_Debug_TxData("<-终端信息上传失败!");	
		Com_Dev_Disconnect( DEF_TcpChanNum );	//断开连接
		Hook_Debug_TxData("<-断开连接!");
		return ;	
	}
	if( COM_ParUpdate & DEF_Update_Bit0 )	 		//增量黑名单更新
	{ 
		Hook_Debug_TxData("->黑名单下载中...");
		if( Com_Bus_TcpBlackDown() == 200 ) 
		{
			Com_Dev_Disconnect( DEF_TcpChanNum );	//断开连接
			Hook_Debug_TxData("<-断开连接!");
			return;	
		}	
	}
	if( COM_ParUpdate & DEF_Update_Bit2 )	 		//票价卡下发
	{
		if( Com_Bus_TcpPriceParaUD() == 200 )	
		{
			Com_Dev_Disconnect( DEF_TcpChanNum );	//断开连接
			Hook_Debug_TxData("<-断开连接!");
			return;	
		}	
	}
	if( COM_ParUpdate & DEF_Update_Bit7 )	 		//银联认证中心公钥参数
	{ 
		if( Com_Bus_TcpUP_CA() == 200 ) 
		{
			Com_Dev_Disconnect( DEF_TcpChanNum );	//断开连接
			Hook_Debug_TxData("<-断开连接!");
			return;	
		}	
	}
	if( COM_ParUpdate & DEF_Update_Bit8 )	 		//银联IC 卡其他参数
	{ 
		if( Com_Bus_TcpUP_IC() == 200 )
		{
			Com_Dev_Disconnect( DEF_TcpChanNum );	//断开连接
			Hook_Debug_TxData("<-断开连接!");
			return;	
		}	
	}
	if( COM_ParUpdate & DEF_Update_Bit11 )	 		//银行卡bin号下载
	{ 
		Hook_Debug_TxData("->BIN号下载中...");
		if( Com_Bus_TcpUPBin( ) == 200 )
		{
			Com_Dev_Disconnect( DEF_TcpChanNum );	//断开连接
			Hook_Debug_TxData("<-断开连接!");
			return;	
		}	
	}	
	if( Com_Bus_TcpRecSend() == 200 )			//记录上传
	{
		Com_Dev_Disconnect( DEF_TcpChanNum );	//断开连接
		Hook_Debug_TxData("<-断开连接!");
		return;	
	}

	Com_Dev_Disconnect( DEF_TcpChanNum );	//断开连接
	Hook_Debug_TxData("<-断开连接!");					
		
}

void Test_Write_OneRec(void)
{
	U32 rcode =0,ver =0;
	static int n = 0;
	U08 buf[12];
	
	get_time();
	TradeInformation.Mtype = 0;
	n++;
	TradeInformation.PullCTC[1]  = n;//交易计数每次加一
	TradeInformation.CardpPE[3] = 1;
	memcpy(TradeInformation.CardASN,"\x01\x02\x01\x02\x01\x02\x01\x02",8);
	APP_WriteRec_CpuRec( DEF_RecMoryBank_Black01, 1, 0 );
	printf("write one record success!\n");
	AnsRec("rec01.bin",1,0);

	ver = 11;
	rcode = APP_UpdateBlacklistVersion( DEF_BlkMemoryBank_Black, ver );
	if(rcode != 0)												
	{
		printf("UpdateBlacklistVersion Error!\n");
	}
	// for(int i = 0;i < 50; i++)
	// {
	// 	COM_TxRxBuf[0] = 0;
	// 	COM_TxRxBuf[1] = 2;
	// 	COM_TxRxBuf[2] = 3;
	// 	COM_TxRxBuf[3] = 4;
	// 	COM_TxRxBuf[4] = 5;
	// 	COM_TxRxBuf[5] = 6;
	// 	COM_TxRxBuf[6] = 7;
	// 	COM_TxRxBuf[7] = 8;
	// 	COM_TxRxBuf[8] = i;
	// 	rcode = APP_SaveBlacklist(DEF_BlkMemoryBank_Black, 
	// 												COM_TxRxBuf, 
	// 												50, 
	// 												NCAP_BlacklistBuffer );
	// 	if(rcode != 0)												
	// 	{
	// 		printf("APP_SaveBlacklist Error!\n");
	// 	}
	// }
	buf[0] = 0;
	memcpy( &buf[1], "\x02\x03\x04\x05\x06\x07\x08\x13", 8 ); 
	if( APP_FindBlacklist( DEF_BlkMoryBank_Black01, (buf+1), buf, NCAP_BlacklistBuffer ) == 0 )
	{														//找到了卡号
		if( buf[0] == 0 )									//并且状态还一样的	
		 { 
			//APP_WriteRec_Blacklist( DEF_RecMoryBank_Black01, TradeInformation.CardASN );	
			printf("<-ok,find Blacklist!\n");			
			//return 0;	
		 }									
		//return 1; 
	}
	else
	{
		printf("<-not find Blacklist!\n");		
	}
	
}

